package com.farewell.blog.controller;

import com.farewell.blog.pojo.Comment;
import com.farewell.blog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @author Farewell
 * @date 2021/2/10
 */
@Controller
public class CommmetntController {
    @Autowired
    private CommentService commentService;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable("blogId")Long blogId, Model model) {
        model.addAttribute("comments", commentService.listCommentByBlogId(blogId));
        return "blog :: commentList";
    }

    @PostMapping("/comments")
    public String saveComment(Comment comment) {
        commentService.saveComment(comment);
        return "redirect:/comments/" + comment.getBlogId();
    }

}
